part of 'win.dart';

class BluetoothServiceWindows extends BluetoothService {
  final DeviceIdentifier remoteId;
  final Guid serviceUuid;
  final bool isPrimary;
  final List<BluetoothCharacteristicWindows> characteristics;
  final List<BluetoothServiceWindows> includedServices;

  BluetoothServiceWindows({
    required this.remoteId,
    required this.serviceUuid,
    required this.isPrimary,
    required this.characteristics,
    required this.includedServices,
  }) : super.fromProto(
          BmBluetoothService(
            remoteId: DeviceIdentifier(remoteId.str),
            serviceUuid: serviceUuid,
            characteristics: [for (final c in characteristics) c.toProto()],
            // isPrimary: isPrimary,
            // includedServices: [for (final s in includedServices) s.toProto()],
            primaryServiceUuid: null, // TODO: API changes
          ),
        );

  static List<BluetoothServiceWindows> fromJson(DeviceIdentifier remoteId, Map<String, dynamic> services) {
    List<BluetoothServiceWindows> _bleServices = [];
    //生成对象BluetoothService
    services.forEach((serviceId, cc) {
      final List<BluetoothCharacteristicWindows> characteristic = [];
      cc.forEach((c) {
        characteristic.add(BluetoothCharacteristicWindows.fromJson(c));
      });
      final service = BluetoothServiceWindows(
        remoteId: remoteId,
        serviceUuid: Guid(serviceId),
        isPrimary: false,
        characteristics: characteristic,
        includedServices: [],
      );
      _bleServices.add(service);
    });
    return _bleServices;
  }

  BluetoothCharacteristicWindows? getCharacteristic(Guid guid) {
    for (var c in characteristics) {
      if (c.uuid == guid) {
        return c;
      }
    }
    return null;
  }
}
